package service

import (
	"fmt"
	"ksd-social-api/commons/base/service"
	"ksd-social-api/commons/page"
	"ksd-social-api/commons/tools"
	"ksd-social-api/modules/channel/mapper"
	"ksd-social-api/modules/channel/model"
	"ksd-social-api/modules/channel/model/vo"
	"ksd-social-api/modules/user/model/context"
	"ksd-social-api/utils"
	"ksd-social-api/utils/rdb"
)

var channelMapper = mapper.ChannelMapper{}
var adminChannelMapper = mapper.AdminChannelMapper{}

type ChannelService struct {
	service.BaseService
}

/*
* 查找栏目
 */
func (service *ChannelService) FindChannels(systemId uint64) *[]vo.ChannelVo {
	cacheKey := fmt.Sprintf("SYSTEM:FindChannels:LIST:%d", systemId)
	jsonString, _ := rdb.RdbGet(cacheKey)
	channelVos := []vo.ChannelVo{}
	tools.JsontoStruct(jsonString, &channelVos)
	if len(channelVos) == 0 {
		channelList, _ := channelMapper.FindChannel(systemId)
		if channelList != nil {
			for _, channel := range channelList {
				channelVo := vo.ChannelVo{}
				utils.CopyProperties(&channelVo, channel)
				channelVos = append(channelVos, channelVo)
			}
		}
		rdb.RdbSetEX(cacheKey, tools.StructToJson(channelVos), 60*60)
	}
	return &channelVos
}

/*
* 查找栏目
 */
func (service *ChannelService) FindChannelZones(systemId uint64) *[]vo.ChannelZoneVo {
	cacheKey := fmt.Sprintf("SYSTEM:FindChannelZones:LIST:%d", systemId)
	jsonString, _ := rdb.RdbGet(cacheKey)
	channelVos := []vo.ChannelZoneVo{}
	tools.JsontoStruct(jsonString, &channelVos)
	if len(channelVos) == 0 {
		channelList, _ := channelMapper.FindChannelZone(systemId)
		if channelList != nil {
			for _, channel := range channelList {
				channelVo := vo.ChannelZoneVo{}
				utils.CopyProperties(&channelVo, channel)
				channelVos = append(channelVos, channelVo)
			}
		}
		rdb.RdbSetEX(cacheKey, tools.StructToJson(channelVos), 60*60*24)
	}
	return &channelVos
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询栏目信息
 */
func (service *ChannelService) GetByIdDetail(id uint64, userId uint64, systemId uint64) *vo.ChannelVo {
	channelVo := vo.ChannelVo{}
	channel, _ := channelMapper.GetById(id, systemId)
	utils.CopyProperties(&channelVo, channel)
	if userId > 0 {
		// 判断用户是否订阅此栏目
		channelVo.BuyFlag = channelMapper.CountUserBuyChannelNo(userId, id, systemId)

	}
	return &channelVo
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询栏目信息
 */
func (service *ChannelService) GetByIdPay(id uint64, systemId uint64) *vo.ChannelVo {
	channelVo := vo.ChannelVo{}
	channel, _ := channelMapper.GetById(id, systemId)
	utils.CopyProperties(&channelVo, channel)
	return &channelVo
}

/**
 * 查询用户是否购买栏目
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
func (service *ChannelService) CountUserBuyChannelNo(userId uint64, channelId uint64, systemId uint64) int {
	return channelMapper.CountUserBuyChannelNo(userId, channelId, systemId)
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc 我订阅的专栏
 */
func (service *ChannelService) FindMyChannelsOrderPage(ctx context.UserCenterContext) *page.Page {
	userBuyChannelPage, _ := channelMapper.FindMyChannelsOrderPage(ctx.UserId, ctx.SystemId, ctx.PageNo, ctx.PageSize)
	userBuyChannelList := userBuyChannelPage.Records.([]model.UserBuyChannel)
	if userBuyChannelList != nil {
		userBuyChannelVos := []vo.UserBuyChannelVo{}
		for _, userBuyChannel := range userBuyChannelList {
			userBuyChannelVo := vo.UserBuyChannelVo{}
			utils.CopyProperties(&userBuyChannelVo, userBuyChannel)
			userBuyChannelVos = append(userBuyChannelVos, userBuyChannelVo)
		}
		userBuyChannelPage.Records = userBuyChannelVos
	}
	return userBuyChannelPage
}
